/*
 * To change this template, choose Tools | Templates and open the template in the editor.
 */
package se.combitech.combiorder.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * @author u013883
 */
@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOMER_GEN")
    @SequenceGenerator(name = "CUSTOMER_GEN", sequenceName = "CUSTOMER_SEQ")
    private Long id;

    private String firstName;
    private String lastName;

    @NotNull(message = "Får ej vara tomt !")
    @NotEmpty(message = "Fältet får inte vara tomt")
    @Pattern(regexp = "^\\d+-\\d+$", message = "Formatet måste vara nn-nnnnnnn")
    private String phone;

    @NotNull
    @NotEmpty(message = "Fältet får inte vara tomt")
    @Email(message = "Formatet är fel !")
    private String email;

    private Boolean smsnotification;

    @XmlTransient
    @OneToMany(mappedBy = "customer", orphanRemoval = true)
    private List<Order> orders;

    @XmlTransient
    @OneToMany(mappedBy = "customer", orphanRemoval = true, cascade = CascadeType.ALL)
    private List<Address> addresses;

    /*
     * Bolierplate code
     */

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getSmsnotification() {
        return smsnotification;
    }

    public void setSmsnotification(Boolean smsnotification) {
        this.smsnotification = smsnotification;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Customer addAddress(Address address) {

        if (addresses == null) {
            addresses = new ArrayList<Address>();
        }

        address.setCustomer(this);
        addresses.add(address);
        return this;
    }

    @Override
    public String toString() {
        return "Customer [id="
            + id
            + ", firstName="
            + firstName
            + ", lastName="
            + lastName
            + ", phone="
            + phone
            + ", email="
            + email
            + ", smsnotification="
            + smsnotification
            + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((addresses == null) ? 0 : addresses.hashCode());
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
        result = prime * result + ((orders == null) ? 0 : orders.hashCode());
        result = prime * result + ((phone == null) ? 0 : phone.hashCode());
        result = prime * result + ((smsnotification == null) ? 0 : smsnotification.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Customer other = (Customer) obj;
        if (addresses == null) {
            if (other.addresses != null)
                return false;
        } else if (!addresses.equals(other.addresses))
            return false;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!firstName.equals(other.firstName))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!lastName.equals(other.lastName))
            return false;
        if (orders == null) {
            if (other.orders != null)
                return false;
        } else if (!orders.equals(other.orders))
            return false;
        if (phone == null) {
            if (other.phone != null)
                return false;
        } else if (!phone.equals(other.phone))
            return false;
        if (smsnotification == null) {
            if (other.smsnotification != null)
                return false;
        } else if (!smsnotification.equals(other.smsnotification))
            return false;
        return true;
    }

}
